var express = require('express');
var router = express.Router();
const {Course,User,Category} = require('../models');
const { where } = require('sequelize');
const { failure, success } = require('../utils/response');

// 前台接口，此接口为课程页面
router.get('/', async function(req, res, next) {
  try {
    const query = req.query
    const currentPage = Math.abs(Number(query.currentPage)) || 1
    const pageSize = Math.abs(Number(query.pageSize)) || 10
    const offset = (currentPage-1)*pageSize

    if(!query.categoryId){
        throw new Error('获取课程列表失败，分类ID不能为空')
    }

    const condition = {
      attributes:{exclude:['categoryId','userId','content']},
      where:{categoryId:query.categoryId},
      order:[['id','DESC']],
      limit:pageSize,
      offset:offset
    }

    const {count,rows} = await Course.findAndCountAll(condition)
    success(res,'查询课程列表成功',{
      courses:rows,
      pagination:{
        total:count,
        currentPage,
        pageSize
      }
    })
  } catch (error) {
    failure(res,error)
  }
});

module.exports = router;
